#include "tdf.h"
#include <cstring>
#include <cstdio>
#include <stack>

tdf::tdf(): tdf_node()
{
    parent = this;
    parent_table = this;
    index = nodesNT.addName((char*)"root");
}
tdf::~tdf(){}

void tdf::clear(){
    nodesNT.clear();
    attributesNT.clear();
    childNodes.clear();
    attrib.clear();
    value.clear();
    index = nodesNT.addName((char*)"root");
}

void tdf::exportTDF(char* filename){

    FILE* outfile = fopen(filename,"w");

    fwrite("TDF",3,1,outfile);

    nodesNT.exportNameTable(outfile);
    attributesNT.exportNameTable(outfile);

    exportNode( outfile );

    fclose( outfile );

}
void tdf::importTDF(char* filename){

    char testString[4];
    testString[3] = 0x00;

    FILE* infile = fopen(filename,"r");
    if( !infile ){
        printf("Erreur: fichier introuvable\n\n");
        return;
    }

    fread(testString, 3, 1, infile);
    if( strcmp(testString,"TDF") ){
        printf("Erreur: format incorrecte\n\n");
        return;
    }

    nodesNT.importNameTable(infile);
    attributesNT.importNameTable(infile);
    importNode( infile );

    uptodate = true;

}

void tdf::exportXML(char* filename){

    FILE* outfile = fopen(filename,"w");

    fprintf(outfile,"<?xml version=\"1.1\"?>\n");
    exportNodeXML(outfile);

    fclose(outfile);

}
